// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DBLiveObjectHelpers.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Entities
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.LiveObjects;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The db live object helpers.
    /// </summary>
    public class DBLiveObjectHelpers
    {
        #region Public Methods

        /// <summary>
        /// List all known applications
        /// </summary>
        /// <returns>
        /// </returns>
        public static IDictionary<Guid, DBLiveObject> ApplicationList()
        {
            // Applications as trees, indexed by guid
            IDictionary<Guid, DBLiveObject> apps = new Dictionary<Guid, DBLiveObject>();

            // All applications (any versions)
            var applications = from a in Singleton<DB>.Instance.Entities.DBLiveObject where a.Type.CompareTo("A") == 0 select a;

            // Create the maps
            foreach (var application in applications)
            {
                apps.Add(application.ID, application);
            }

            // All done
            return apps;
        }

        /// <summary>
        /// List the services of a given application (referenced by its id)
        /// </summary>
        /// <param name="applicationGuid">
        /// The applicationGuid.
        /// </param>
        /// <returns>
        /// </returns>
        public static IDictionary<Guid, DBLiveObject> ApplicationServicesList(Guid applicationGuid)
        {
            // Applications as trees, indexed by guid
            IDictionary<Guid, DBLiveObject> svcs = new Dictionary<Guid, DBLiveObject>();

            // All applications (any versions)
            var applications = from a in Singleton<DB>.Instance.Entities.DBLiveObject where a.ID == applicationGuid select a;

            var application = applications.First();
            foreach (var service in application.DBLiveObject1)
            {
                svcs.Add(service.ID, service);
            }

            // All done
            return svcs;
        }

        /// <summary>
        /// The create descriptor.
        /// </summary>
        /// <param name="dbInstance">
        /// The dbInstance.
        /// </param>
        /// <param name="parentInstanceId">
        /// The parentInstanceId.
        /// </param>
        /// <returns>
        /// </returns>
        public static ILiveObjectDescriptor CreateDescriptor(DBLiveObject dbInstance, Guid parentInstanceId)
        {
            dbInstance.DBContainerReference.Load();

            if (dbInstance.Location.CompareTo("L") == 0)
            {
                if (dbInstance.ListenURL == null)
                {
                    return new LiveObjectDescriptor.Local(
                        dbInstance.ID, 
                        parentInstanceId, 
                        dbInstance.DBConfigurationID, 
                        dbInstance.Name, 
                        dbInstance.Label, 
                        dbInstance.InterfaceFullTypeName, 
                        dbInstance.ClassFullTypeName, 
                        dbInstance.DBContainer.ID);
                }
                else
                {
                    return new LiveObjectDescriptor.Local(
                        dbInstance.ID, 
                        parentInstanceId, 
                        dbInstance.DBConfigurationID, 
                        dbInstance.Name, 
                        dbInstance.Label, 
                        dbInstance.InterfaceFullTypeName, 
                        dbInstance.ClassFullTypeName, 
                        dbInstance.DBContainer.ID, 
                        dbInstance.ListenURL);
                }
            }
            else if (dbInstance.Location.CompareTo("R") == 0)
            {
                return new LiveObjectDescriptor.Remote(dbInstance.ID, parentInstanceId, dbInstance.Name, dbInstance.InterfaceFullTypeName, dbInstance.URL);
            }

            return null;
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="dbLiveObject">
        /// The dbLiveObject.
        /// </param>
        public static void Delete(DBLiveObject dbLiveObject)
        {
            try
            {
                var configId = dbLiveObject.DBConfigurationID;

                // Delete the configuration
                DBConfigurationHelpers.DeleteDBConfiguration(configId);

                // Delete the object
                Singleton<DB>.Instance.Entities.DeleteObject(dbLiveObject);

                // Save changes
                var n = Singleton<DB>.Instance.Commit();
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// The make label.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        /// <returns>
        /// The make label.
        /// </returns>
        public static string MakeLabel(object o)
        {
            return string.Empty + o.GetHashCode();
        }

        #endregion
    }
}